/*
 * Copyright (C) 2023 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#include "dsa_asy_key_generator_openssl.c"
#include "dsa_openssl.c"
#include "params_parser.h"

using namespace std;
using namespace testing::ext;

namespace {
class CryptoDsaExceptionTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
};

void CryptoDsaExceptionTest::SetUpTestCase() {}
void CryptoDsaExceptionTest::TearDownTestCase() {}
void CryptoDsaExceptionTest::SetUp() {}
void CryptoDsaExceptionTest::TearDown() {}

static const char *GetMockClass(void)
{
    return "HcfAsyKeyGenerator";
}

HcfObjectBase g_obj = {
    .getClass = GetMockClass,
    .destroy = nullptr
};

static const char *g_algNameDSA = "DSA";

constexpr uint32_t DSA2048_PUB_SIZE = 256;
constexpr uint32_t DSA2048_P_SIZE = 256;
constexpr uint32_t DSA2048_Q_SIZE = 20;
constexpr uint32_t DSA2048_G_SIZE = 256;

static const bool IS_BIG_ENDIAN = IsBigEndian();

static unsigned char g_dsa2048PBigE[] = {
    0x00, 0x25, 0xFA, 0xAD, 0xF4, 0x8E, 0xB9, 0xE5, 0x99, 0xF3, 0x5D, 0x6F,
    0x4F, 0x83, 0x34, 0xE2, 0x7E, 0xCF, 0x6F, 0xBF, 0x30, 0xAF, 0x6F, 0x81,
    0xEB, 0xF8, 0xC4, 0x13, 0xD9, 0xA0, 0x5D, 0x8B, 0x5C, 0x8E, 0xDC, 0xC2,
    0x1D, 0x0B, 0x41, 0x32, 0xB0, 0x1F, 0xFE, 0xEF, 0x0C, 0xC2, 0xA2, 0x7E,
    0x68, 0x5C, 0x28, 0x21, 0xE9, 0xF5, 0xB1, 0x58, 0x12, 0x63, 0x4C, 0x19,
    0x4E, 0xFF, 0x02, 0x4B, 0x92, 0xED, 0xD2, 0x07, 0x11, 0x4D, 0x8C, 0x58,
    0x16, 0x5C, 0x55, 0x8E, 0xAD, 0xA3, 0x67, 0x7D, 0xB9, 0x86, 0x6E, 0x0B,
    0xE6, 0x54, 0x6F, 0x40, 0xAE, 0x0E, 0x67, 0x4C, 0xF9, 0x12, 0x5B, 0x3C,
    0x08, 0x7A, 0xF7, 0xFC, 0x67, 0x86, 0x69, 0xE7, 0x0A, 0x94, 0x40, 0xBF,
    0x8B, 0x76, 0xFE, 0x26, 0xD1, 0xF2, 0xA1, 0x1A, 0x84, 0xA1, 0x43, 0x56,
    0x28, 0xBC, 0x9A, 0x5F, 0xD7, 0x3B, 0x69, 0x89, 0x8A, 0x36, 0x2C, 0x51,
    0xDF, 0x12, 0x77, 0x2F, 0x57, 0x7B, 0xA0, 0xAA, 0xDD, 0x7F, 0xA1, 0x62,
    0x3B, 0x40, 0x7B, 0x68, 0x1A, 0x8F, 0x0D, 0x38, 0xBB, 0x21, 0x5D, 0x18,
    0xFC, 0x0F, 0x46, 0xF7, 0xA3, 0xB0, 0x1D, 0x23, 0xC3, 0xD2, 0xC7, 0x72,
    0x51, 0x18, 0xDF, 0x46, 0x95, 0x79, 0xD9, 0xBD, 0xB5, 0x19, 0x02, 0x2C,
    0x87, 0xDC, 0xE7, 0x57, 0x82, 0x7E, 0xF1, 0x8B, 0x06, 0x3D, 0x00, 0xA5,
    0x7B, 0x6B, 0x26, 0x27, 0x91, 0x0F, 0x6A, 0x77, 0xE4, 0xD5, 0x04, 0xE4,
    0x12, 0x2C, 0x42, 0xFF, 0xD2, 0x88, 0xBB, 0xD3, 0x92, 0xA0, 0xF9, 0xC8,
    0x51, 0x64, 0x14, 0x5C, 0xD8, 0xF9, 0x6C, 0x47, 0x82, 0xB4, 0x1C, 0x7F,
    0x09, 0xB8, 0xF0, 0x25, 0x83, 0x1D, 0x3F, 0x3F, 0x05, 0xB3, 0x21, 0x0A,
    0x5D, 0xA7, 0xD8, 0x54, 0xC3, 0x65, 0x7D, 0xC3, 0xB0, 0x1D, 0xBF, 0xAE,
    0xF8, 0x68, 0xCF, 0x00,
};

static unsigned char g_dsa2048PLittleE[] = {
    0x00, 0xcf, 0x68, 0xf8, 0xae, 0xbf, 0x1d, 0xb0, 0xc3, 0x7d, 0x65, 0xc3,
    0x54, 0xd8, 0xa7, 0x5d, 0x0a, 0x21, 0xb3, 0x05, 0x3f, 0x3f, 0x1d, 0x83,
    0x25, 0xf0, 0xb8, 0x09, 0x7f, 0x1c, 0xb4, 0x82, 0x47, 0x6c, 0xf9, 0xd8,
    0x5c, 0x14, 0x64, 0x51, 0xc8, 0xf9, 0xa0, 0x92, 0xd3, 0xbb, 0x88, 0xd2,
    0xff, 0x42, 0x2c, 0x12, 0xe4, 0x04, 0xd5, 0xe4, 0x77, 0x6a, 0x0f, 0x91,
    0x27, 0x26, 0x6b, 0x7b, 0xa5, 0x00, 0x3d, 0x06, 0x8b, 0xf1, 0x7e, 0x82,
    0x57, 0xe7, 0xdc, 0x87, 0x2c, 0x02, 0x19, 0xb5, 0xbd, 0xd9, 0x79, 0x95,
    0x46, 0xdf, 0x18, 0x51, 0x72, 0xc7, 0xd2, 0xc3, 0x23, 0x1d, 0xb0, 0xa3,
    0xf7, 0x46, 0x0f, 0xfc, 0x18, 0x5d, 0x21, 0xbb, 0x38, 0x0d, 0x8f, 0x1a,
    0x68, 0x7b, 0x40, 0x3b, 0x62, 0xa1, 0x7f, 0xdd, 0xaa, 0xa0, 0x7b, 0x57,
    0x2f, 0x77, 0x12, 0xdf, 0x51, 0x2c, 0x36, 0x8a, 0x89, 0x69, 0x3b, 0xd7,
    0x5f, 0x9a, 0xbc, 0x28, 0x56, 0x43, 0xa1, 0x84, 0x1a, 0xa1, 0xf2, 0xd1,
    0x26, 0xfe, 0x76, 0x8b, 0xbf, 0x40, 0x94, 0x0a, 0xe7, 0x69, 0x86, 0x67,
    0xfc, 0xf7, 0x7a, 0x08, 0x3c, 0x5b, 0x12, 0xf9, 0x4c, 0x67, 0x0e, 0xae,
    0x40, 0x6f, 0x54, 0xe6, 0x0b, 0x6e, 0x86, 0xb9, 0x7d, 0x67, 0xa3, 0xad,
    0x8e, 0x55, 0x5c, 0x16, 0x58, 0x8c, 0x4d, 0x11, 0x07, 0xd2, 0xed, 0x92,
    0x4b, 0x02, 0xff, 0x4e, 0x19, 0x4c, 0x63, 0x12, 0x58, 0xb1, 0xf5, 0xe9,
    0x21, 0x28, 0x5c, 0x68, 0x7e, 0xa2, 0xc2, 0x0c, 0xef, 0xfe, 0x1f, 0xb0,
    0x32, 0x41, 0x0b, 0x1d, 0xc2, 0xdc, 0x8e, 0x5c, 0x8b, 0x5d, 0xa0, 0xd9,
    0x13, 0xc4, 0xf8, 0xeb, 0x81, 0x6f, 0xaf, 0x30, 0xbf, 0x6f, 0xcf, 0x7e,
    0xe2, 0x34, 0x83, 0x4f, 0x6f, 0x5d, 0xf3, 0x99, 0xe5, 0xb9, 0x8e, 0xf4,
    0xad, 0xfa, 0x25, 0x00,
};

static unsigned char g_dsa2048QBigE[] = {
    0x00, 0xE7, 0x33, 0x4D, 0xD3, 0x94, 0x3E, 0x0B, 0xDB, 0x62, 0x74, 0xC6,
    0xA1, 0x08, 0xDD, 0x19, 0xA3, 0x75, 0x17, 0x00,
};

static unsigned char g_dsa2048QLittleE[] = {
    0x00, 0x17, 0x75, 0xa3, 0x19, 0xdd, 0x08, 0xa1, 0xc6, 0x74, 0x62, 0xdb,
    0x0b, 0x3e, 0x94, 0xd3, 0x4d, 0x33, 0xe7, 0x00,
};

static unsigned char g_dsa2048GBigE[] = {
    0x00, 0x78, 0x16, 0x59, 0x34, 0x63, 0xF4, 0xF3, 0x92, 0xFC, 0xB5, 0xA5,
    0x4F, 0x13, 0xDE, 0x2F, 0x1C, 0xA4, 0x3C, 0xAE, 0xAD, 0x38, 0x3F, 0x7E,
    0x90, 0xBF, 0x96, 0xA6, 0xAE, 0x25, 0x90, 0x72, 0xF5, 0x8E, 0x80, 0x0C,
    0x39, 0x1C, 0xD9, 0xEC, 0xBA, 0x90, 0x5B, 0x3A, 0xE8, 0x58, 0x6C, 0x9E,
    0x30, 0x42, 0x37, 0x02, 0x31, 0x82, 0xBC, 0x6A, 0xDF, 0x6A, 0x09, 0x29,
    0xE3, 0xC0, 0x46, 0xD1, 0xCB, 0x85, 0xEC, 0x0C, 0x30, 0x5E, 0xEA, 0xC8,
    0x39, 0x8E, 0x22, 0x9F, 0x22, 0x10, 0xD2, 0x34, 0x61, 0x68, 0x37, 0x3D,
    0x2E, 0x4A, 0x5B, 0x9A, 0xF5, 0xC1, 0x48, 0xC6, 0xF6, 0xDC, 0x63, 0x1A,
    0xD3, 0x96, 0x64, 0xBA, 0x34, 0xC9, 0xD1, 0xA0, 0xD1, 0xAE, 0x6C, 0x2F,
    0x48, 0x17, 0x93, 0x14, 0x43, 0xED, 0xF0, 0x21, 0x30, 0x19, 0xC3, 0x1B,
    0x5F, 0xDE, 0xA3, 0xF0, 0x70, 0x78, 0x18, 0xE1, 0xA8, 0xE4, 0xEE, 0x2E,
    0x00, 0xA5, 0xE4, 0xB3, 0x17, 0xC8, 0x0C, 0x7D, 0x6E, 0x42, 0xDC, 0xB7,
    0x46, 0x00, 0x36, 0x4D, 0xD4, 0x46, 0xAA, 0x3D, 0x3C, 0x46, 0x89, 0x40,
    0xBF, 0x1D, 0x84, 0x77, 0x0A, 0x75, 0xF3, 0x87, 0x1D, 0x08, 0x4C, 0xA6,
    0xD1, 0xA9, 0x1C, 0x1E, 0x12, 0x1E, 0xE1, 0xC7, 0x30, 0x28, 0x76, 0xA5,
    0x7F, 0x6C, 0x85, 0x96, 0x2B, 0x6F, 0xDB, 0x80, 0x66, 0x26, 0xAE, 0xF5,
    0x93, 0xC7, 0x8E, 0xAE, 0x9A, 0xED, 0xE4, 0xCA, 0x04, 0xEA, 0x3B, 0x72,
    0xEF, 0xDC, 0x87, 0xED, 0x0D, 0xA5, 0x4C, 0x4A, 0xDD, 0x71, 0x22, 0x64,
    0x59, 0x69, 0x4E, 0x8E, 0xBF, 0x43, 0xDC, 0xAB, 0x8E, 0x66, 0xBB, 0x01,
    0xB6, 0xF4, 0xE7, 0xFD, 0xD2, 0xAD, 0x9F, 0x36, 0xC1, 0xA0, 0x29, 0x99,
    0xD1, 0x96, 0x70, 0x59, 0x06, 0x78, 0x35, 0xBD, 0x65, 0x55, 0x52, 0x9E,
    0xF8, 0xB2, 0xE5, 0x00,
};

static unsigned char g_dsa2048GLittleE[] = {
    0x00, 0xe5, 0xb2, 0xf8, 0x9e, 0x52, 0x55, 0x65, 0xbd, 0x35, 0x78, 0x06,
    0x59, 0x70, 0x96, 0xd1, 0x99, 0x29, 0xa0, 0xc1, 0x36, 0x9f, 0xad, 0xd2,
    0xfd, 0xe7, 0xf4, 0xb6, 0x01, 0xbb, 0x66, 0x8e, 0xab, 0xdc, 0x43, 0xbf,
    0x8e, 0x4e, 0x69, 0x59, 0x64, 0x22, 0x71, 0xdd, 0x4a, 0x4c, 0xa5, 0x0d,
    0xed, 0x87, 0xdc, 0xef, 0x72, 0x3b, 0xea, 0x04, 0xca, 0xe4, 0xed, 0x9a,
    0xae, 0x8e, 0xc7, 0x93, 0xf5, 0xae, 0x26, 0x66, 0x80, 0xdb, 0x6f, 0x2b,
    0x96, 0x85, 0x6c, 0x7f, 0xa5, 0x76, 0x28, 0x30, 0xc7, 0xe1, 0x1e, 0x12,
    0x1e, 0x1c, 0xa9, 0xd1, 0xa6, 0x4c, 0x08, 0x1d, 0x87, 0xf3, 0x75, 0x0a,
    0x77, 0x84, 0x1d, 0xbf, 0x40, 0x89, 0x46, 0x3c, 0x3d, 0xaa, 0x46, 0xd4,
    0x4d, 0x36, 0x00, 0x46, 0xb7, 0xdc, 0x42, 0x6e, 0x7d, 0x0c, 0xc8, 0x17,
    0xb3, 0xe4, 0xa5, 0x00, 0x2e, 0xee, 0xe4, 0xa8, 0xe1, 0x18, 0x78, 0x70,
    0xf0, 0xa3, 0xde, 0x5f, 0x1b, 0xc3, 0x19, 0x30, 0x21, 0xf0, 0xed, 0x43,
    0x14, 0x93, 0x17, 0x48, 0x2f, 0x6c, 0xae, 0xd1, 0xa0, 0xd1, 0xc9, 0x34,
    0xba, 0x64, 0x96, 0xd3, 0x1a, 0x63, 0xdc, 0xf6, 0xc6, 0x48, 0xc1, 0xf5,
    0x9a, 0x5b, 0x4a, 0x2e, 0x3d, 0x37, 0x68, 0x61, 0x34, 0xd2, 0x10, 0x22,
    0x9f, 0x22, 0x8e, 0x39, 0xc8, 0xea, 0x5e, 0x30, 0x0c, 0xec, 0x85, 0xcb,
    0xd1, 0x46, 0xc0, 0xe3, 0x29, 0x09, 0x6a, 0xdf, 0x6a, 0xbc, 0x82, 0x31,
    0x02, 0x37, 0x42, 0x30, 0x9e, 0x6c, 0x58, 0xe8, 0x3a, 0x5b, 0x90, 0xba,
    0xec, 0xd9, 0x1c, 0x39, 0x0c, 0x80, 0x8e, 0xf5, 0x72, 0x90, 0x25, 0xae,
    0xa6, 0x96, 0xbf, 0x90, 0x7e, 0x3f, 0x38, 0xad, 0xae, 0x3c, 0xa4, 0x1c,
    0x2f, 0xde, 0x13, 0x4f, 0xa5, 0xb5, 0xfc, 0x92, 0xf3, 0xf4, 0x63, 0x34,
    0x59, 0x16, 0x78, 0x00,
};

static HcfBigInteger p_BN = { .data = IS_BIG_ENDIAN ? g_dsa2048PBigE : g_dsa2048PLittleE, .len = DSA2048_P_SIZE };
static HcfBigInteger q_BN = { .data = IS_BIG_ENDIAN ? g_dsa2048QBigE : g_dsa2048QLittleE, .len = DSA2048_Q_SIZE };
static HcfBigInteger g_BN = { .data = IS_BIG_ENDIAN ? g_dsa2048GBigE : g_dsa2048GLittleE, .len = DSA2048_G_SIZE };

static HcfBigInteger p_EXBN = { .data = IS_BIG_ENDIAN ? g_dsa2048PBigE : g_dsa2048PLittleE, .len = DSA2048_P_SIZE + 1 };

static HcfAsyKeyParamsSpec asySpecComm = {
    .algName = const_cast<char *>(g_algNameDSA),
    .specType = HCF_COMMON_PARAMS_SPEC
};
static HcfAsyKeyParamsSpec asySpecPk = {
    .algName = const_cast<char *>(g_algNameDSA),
    .specType = HCF_PUBLIC_KEY_SPEC
};
static HcfAsyKeyParamsSpec asySpecKeyPair = {
    .algName = const_cast<char *>(g_algNameDSA),
    .specType = HCF_KEY_PAIR_SPEC
};

static HcfDsaCommParamsSpec dsaCommonSpec = { .base = asySpecComm, .p = p_BN, .q = q_BN, .g = g_BN };
static HcfDsaCommParamsSpec dsaCommonSpecException = { .base = asySpecComm, .p = p_EXBN, .q = q_BN, .g = g_BN };
HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest001, TestSize.Level0)
{
    BIGNUM *p = nullptr;
    BIGNUM *q = nullptr;
    BIGNUM *g = nullptr;

    HcfResult ret = BigIntegerToBigNum(&p_BN, &p);
    ASSERT_EQ(ret, HCF_SUCCESS);

    ret = BigIntegerToBigNum(&q_BN, &q);
    ASSERT_EQ(ret, HCF_SUCCESS);

    ret = BigIntegerToBigNum(&g_BN, &g);
    ASSERT_EQ(ret, HCF_SUCCESS);

    FreeCommSpecBn(p, q, g);

    FreeCtx(nullptr, nullptr, nullptr);
    FreeCommSpecBn(nullptr, nullptr, nullptr);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest002, TestSize.Level0)
{
    DestroyDsaKeyGeneratorSpiImpl(&g_obj);
    DestroyDsaKeyGeneratorSpiImpl(nullptr);

    DestroyDsaPriKey(&g_obj);

    GetDsaPriKeyAlgorithm(nullptr);
    GetDsaPriKeyAlgorithm((HcfKey *)&g_obj);

    HcfResult ret = GetDsaPriKeyEncoded(nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = GetDsaPriKeyEncoded((HcfKey *)&g_obj, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfBlob blob = { .data = nullptr, .len = 0 };
    ret = GetDsaPriKeyEncoded((HcfKey *)&g_obj, &blob);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    (void)GetDsaPubKeyFormat((HcfKey *)&g_obj);
    (void)GetDsaPriKeyFormat(nullptr);
    (void)GetDsaPriKeyFormat((HcfKey *)&g_obj);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest003, TestSize.Level0)
{
    HcfResult ret = GetBigIntegerSpecFromDsaPubKey(nullptr, DSA_P_BN, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = GetBigIntegerSpecFromDsaPubKey((const HcfPubKey *)&g_obj, DSA_P_BN, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfBigInteger p = { .data = nullptr, .len = 0 };
    GetBigIntegerSpecFromDsaPubKey((const HcfPubKey *)&g_obj, DSA_P_BN, &p);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest004, TestSize.Level0)
{
    HcfResult ret = GetBigIntegerSpecFromDsaPriKey(nullptr, DSA_P_BN, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = GetBigIntegerSpecFromDsaPriKey((const HcfPriKey *)&g_obj, DSA_P_BN, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfBigInteger p = { .data = nullptr, .len = 0 };
    GetBigIntegerSpecFromDsaPriKey((const HcfPriKey *)&g_obj, DSA_P_BN, &p);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest005, TestSize.Level0)
{
    ClearDsaPriKeyMem(nullptr);
    ClearDsaPriKeyMem((HcfPriKey *)&g_obj);

    HcfOpensslDsaPubKey *pubKey = nullptr;
    HcfOpensslDsaPriKey *priKey = nullptr;
    HcfResult ret = GenerateDsaPubAndPriKey(DSA2048_PUB_SIZE + 1, &pubKey, &priKey);
    ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest006, TestSize.Level0)
{
    BIGNUM *p = nullptr;
    BIGNUM *q = nullptr;

    HcfResult ret = ConvertCommSpec2Bn(&dsaCommonSpec, nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);

    ret = ConvertCommSpec2Bn(&dsaCommonSpec, &p, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);

    ret = ConvertCommSpec2Bn(&dsaCommonSpec, &p, &q, nullptr);
    ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest007, TestSize.Level0)
{
    HcfKeyPair *keyPair = nullptr;
    HcfResult ret = CreateDsaKeyPairByCommSpec(&dsaCommonSpecException, &keyPair);
    ASSERT_EQ(ret, HCF_ERR_CRYPTO_OPERATION);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest008, TestSize.Level0)
{
    HcfOpensslDsaPubKey *pubKey = nullptr;
    HcfOpensslDsaPriKey *priKey = nullptr;
    HcfResult ret = ConvertDsaPubAndPriKey(nullptr, nullptr, &pubKey, &priKey);
    ASSERT_EQ(ret, HCF_SUCCESS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest009, TestSize.Level0)
{
    HcfKeyPair *keyPair = nullptr;
    HcfResult ret = EngineGenerateDsaKeyPair((HcfAsyKeyGeneratorSpi *)&g_obj, &keyPair);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineGenerateDsaKeyPair((HcfAsyKeyGeneratorSpi *)&g_obj, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest010, TestSize.Level0)
{
    HcfResult ret = EngineConvertDsaKey(nullptr, nullptr, nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineConvertDsaKey((HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfKeyPair *keyPair = nullptr;
    ret = EngineConvertDsaKey((HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr, nullptr, &keyPair);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest011, TestSize.Level0)
{
    HcfResult ret = EngineGenerateDsaKeyPairBySpec(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineGenerateDsaKeyPairBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineGenerateDsaKeyPairBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfKeyPair *keyPair = nullptr;
    ret = EngineGenerateDsaKeyPairBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, &keyPair);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest012, TestSize.Level0)
{
    HcfResult ret = EngineGenerateDsaPubKeyBySpec(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineGenerateDsaPubKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineGenerateDsaPubKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecPk, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfPubKey *pk = nullptr;
    ret = EngineGenerateDsaPubKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecPk, &pk);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest013, TestSize.Level0)
{
    HcfResult ret = EngineGenerateDsaPriKeyBySpec(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineGenerateDsaPriKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineGenerateDsaPriKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfPriKey *sk = nullptr;
    ret = EngineGenerateDsaPriKeyBySpec((const HcfAsyKeyGeneratorSpi *)&g_obj, &asySpecKeyPair, &sk);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest014, TestSize.Level0)
{
    HcfAsyKeyGenParams params = {
        .algo = HCF_ALG_DSA,
        .bits = 0,
        .primes = HCF_OPENSSL_PRIMES_2
    };

    HcfResult ret = HcfAsyKeyGeneratorSpiDsaCreate(nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = HcfAsyKeyGeneratorSpiDsaCreate(&params, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest100, TestSize.Level0)
{
    bool ret = IsSignInitInputValid(nullptr, nullptr);
    ASSERT_EQ(ret, false);

    ret = IsSignInitInputValid((HcfSignSpi *)&g_obj, nullptr);
    ASSERT_EQ(ret, false);

    HcfPriKey *sk = nullptr;
    ret = IsSignInitInputValid((HcfSignSpi *)&g_obj, sk);
    ASSERT_EQ(ret, false);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest101, TestSize.Level0)
{
    bool ret = IsVerifyInitInputValid(nullptr, nullptr);
    ASSERT_EQ(ret, false);

    ret = IsVerifyInitInputValid((HcfVerifySpi *)&g_obj, nullptr);
    ASSERT_EQ(ret, false);

    HcfPubKey *pk = nullptr;
    ret = IsVerifyInitInputValid((HcfVerifySpi *)&g_obj, pk);
    ASSERT_EQ(ret, false);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest102, TestSize.Level0)
{
    bool ret = IsSignDoFinalInputValid(nullptr, nullptr);
    ASSERT_EQ(ret, false);

    ret = IsSignDoFinalInputValid((HcfSignSpi *)&g_obj, nullptr);
    ASSERT_EQ(ret, false);

    HcfBlob signatureData = { .data = nullptr, .len = 0 };
    ret = IsSignDoFinalInputValid((HcfSignSpi *)&g_obj, &signatureData);
    ASSERT_EQ(ret, false);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest103, TestSize.Level0)
{
    bool ret = IsVerifyDoFinalInputValid(nullptr, nullptr);
    ASSERT_EQ(ret, false);

    ret = IsVerifyDoFinalInputValid((HcfVerifySpi *)&g_obj, nullptr);
    ASSERT_EQ(ret, false);

    HcfBlob signatureData = { .data = nullptr, .len = 0 };
    ret = IsVerifyDoFinalInputValid((HcfVerifySpi *)&g_obj, &signatureData);
    ASSERT_EQ(ret, false);

    DestroyDsaSign(nullptr);
    DestroyDsaSign((HcfObjectBase *)&g_obj);

    DestroyDsaVerify(nullptr);
    DestroyDsaVerify((HcfObjectBase *)&g_obj);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest104, TestSize.Level0)
{
    HcfResult ret = EngineDsaSignInit(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineDsaSignWithoutDigestInit(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineDsaVerifyInit(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineDsaVerifyWithoutDigestInit(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest105, TestSize.Level0)
{
    HcfResult ret = EngineDsaSignUpdate(nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineDsaSignUpdate((HcfSignSpi *)&g_obj, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfBlob data = { .data = nullptr, .len = 0 };
    ret = EngineDsaSignUpdate((HcfSignSpi *)&g_obj, &data);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest106, TestSize.Level0)
{
    HcfResult ret = EngineDsaVerifyUpdate(nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineDsaVerifyUpdate((HcfVerifySpi *)&g_obj, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfBlob data = { .data = nullptr, .len = 0 };
    ret = EngineDsaVerifyUpdate((HcfVerifySpi *)&g_obj, &data);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest107, TestSize.Level0)
{
    HcfResult ret = EngineDsaSignDoFinal(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = EngineDsaSignWithoutDigestDoFinal(nullptr, nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    (void)EngineDsaVerifyDoFinal(nullptr, nullptr, nullptr);

    (void)EngineDsaVerifyWithoutDigestDoFinal(nullptr, nullptr, nullptr);
}

HWTEST_F(CryptoDsaExceptionTest, CryptoDsaExceptionTest108, TestSize.Level0)
{
    HcfResult ret = HcfSignSpiDsaCreate(nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    HcfSignatureParams params = {
        .algo = HCF_ALG_DSA,
        .padding = HCF_ALG_NOPADDING,
        .md = HCF_OPENSSL_DIGEST_SHA256,
        .mgf1md = HCF_OPENSSL_DIGEST_SHA256
    };
    ret = HcfSignSpiDsaCreate(&params, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = HcfVerifySpiDsaCreate(nullptr, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);

    ret = HcfVerifySpiDsaCreate(&params, nullptr);
    ASSERT_EQ(ret, HCF_INVALID_PARAMS);
}
}
